package com.ea.eadp.nexus.test.pageDefinition.newconsole;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.testng.Assert;

public class ContactPreferencesPage extends ConsoleBasePage {
    public final static String shareInfoCheckBox = "shareInfo";
    public final static String contactMeCheckBox = "contactMe";

    public ContactPreferencesPage(WebDriver driver) {
        super(driver, ContactPreferencesPage.class, "label-contactMe");
    }

    public void withoutSelectingShareInfo() {
        log.info("Do NOT select ShareInfo CheckBox");
        try{
            driver.findElement(By.id(shareInfoCheckBox));
            sendControllerKey(Keys.DOWN);
        } catch (Exception e) {
        } finally {
            sendControllerKey('A');
        }
    }

    public void selectShareInfo() {
        log.info("Select ShareInfo CheckBox");
        selectCheckBox(shareInfoCheckBox);
        sendControllerKey(Keys.DOWN);
        sendControllerKey('A');
    }

    public void verifyShareInfoNotExisting() {
        try {
            driver.findElement(By.id(shareInfoCheckBox));
            Assert.fail("Share info should not exist.");
        } catch (Exception e) {
        }
    }

    public void verifyShareInfoExisting() {
        driver.findElement(By.id(shareInfoCheckBox));
    }

    public void selectOptins(boolean contactMe, boolean shareInfo) {
        log.info("Select contactMe:" + String.valueOf(contactMe)+ ", ShareInfo:" +String.valueOf(shareInfo));
        sendControllerKey(Keys.UP);

        if( getStatusForCheckBox(contactMeCheckBox) != contactMe){
            selectCheckBox(contactMeCheckBox);
        }
        sendControllerKey(Keys.DOWN);
        if( getStatusForCheckBox(shareInfoCheckBox) != shareInfo){
            selectCheckBox(shareInfoCheckBox);
        }
        sendControllerKey(Keys.DOWN);
        sendControllerKey('A');
    }

    public String getTitle() {
        return getElementStringByXPath("//div[@id='panel-preference']//span[@class='title']");
    }

    public String getContactMeLabel() {
        return getElementStringByXPath("//label[@id='label-contactMe']");
    }

    public String getShareInfoLabel() {
        return getElementStringByXPath("//label[@id='label-shareInfo']");
    }

    public void SelectShareInfo(boolean checked) {
        WebElement checkboxParent = driver.findElement(By.id(shareInfoCheckBox)).findElement(By.xpath(".."));
        if (!checkboxParent.getAttribute("class").contains("checked")) {
            sendControllerKey('A');
        }
    }

    public String getContactMeTooltip() {
        List<WebElement> webElements = driver.findElements(By.xpath("//div[@id='panel-preference']/div[2]/div[2]/div[1]/ul/li[1]/div/div/div/div/div[2]/span")) ;

        if(webElements.size() > 0){
            return webElements.get(0).getAttribute("innerHTML");
        }
        return "";
    }

    public String getShareInfoTooltip() {
        List<WebElement> webElements = driver.findElements(By.xpath("//div[@id='share-info-box-container']/div/div/div/div/div[2]/span")) ;
        if(webElements.size() > 0){
            return webElements.get(0).getAttribute("innerHTML");
        }
        webElements = driver.findElements(By.xpath("//div[@id='shareInfoTooltip']/span")) ;
        if(webElements.size() > 0){
            return webElements.get(0).getAttribute("innerHTML");
        }
        return "";
    }
}
